// @ts-nocheck
import { IApplication, NsGraph, XFlowGraphCommands } from '@antv/xflow'

const initGraph = async (app: IApplication, data: NsGraph.IGraphData) => {
  try {
    // 1. 执行布局算法
    const res = await app.executeCommand<
      NsGraphCmd.GraphLayout.IArgs,
      NsGraphCmd.GraphLayout.IResult
    >(XFlowGraphCommands.GRAPH_LAYOUT.id, {
      layoutType: 'dagre',
      layoutOptions: {
        type: 'dagre',
        /** 布局方向 */
        rankdir: 'TB',
        /** 节点间距 */
        nodesep: 60,
        /** 层间距 */
        ranksep: 30
      },
      graphData: data
    })
    // 2. 画布内容渲染
    const { graphData } = res.contextProvider().getResult()
    await app.executeCommand<NsGraphCmd.GraphRender.IArgs>(
      XFlowGraphCommands.GRAPH_RENDER.id,
      {
        graphData: graphData
      }
    )
    // 3. 居中
    await app.executeCommand<NsGraphCmd.GraphZoom.IArgs>(
      XFlowGraphCommands.GRAPH_ZOOM.id,
      {
        // factor: 'real'
        factor: 'real'
      }
    )
  } catch (error) {
    console.log(error)
  }
}
export { initGraph }
